package edu.uclmgmu.bacteriosec.aspects.java.io.FileOutputStream;

import java.io.FileOutputStream;

import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect FileOutputStream_Inverter {

	void around(java.io.FileOutputStream caller, byte[] a, int b, int c) : target(caller) && args(a, b, c) &&  
			call(* FileOutputStream.write(byte[], int, int)) && 
			!cflow(within(FileOutputStream_Inverter)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				byte[] theArray=new byte[a.length];
				int cont=0;
				for (int i=a.length-1; i>=0; i--) {
					theArray[cont++]=a[i];
				}
				a=theArray;
			}
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a, b, c);
		} else {
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a, b, c);
		}
	}

		void around(java.io.FileOutputStream caller, byte[] a) : target(caller) && args(a) &&  
			call(* FileOutputStream.write(byte[])) && 
			!cflow(within(FileOutputStream_Inverter)) && !within(edu.uclmgmu.bacteriosec.aspects.common.Store) {
		String currentJoinPoint = Store.getCurrentjoinPoint();
		String signature=thisJoinPointStaticPart.getSignature().toString();
		if (currentJoinPoint.equals(signature)) {
			int numberOfCallInTurn = Store.getNumberOfCallInTurn(signature);
			int numberOfCurrentCall = Store.getNumberOfCurrentCall(signature);
			if (numberOfCallInTurn == numberOfCurrentCall) {
				byte[] theArray=new byte[a.length];
				int cont=0;
				for (int i=a.length-1; i>=0; i--) {
					theArray[cont++]=a[i];
				}
				a=theArray;
			}
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a);
		} else {
			Store.updateNumberOfCurrentCall(signature);
			proceed(caller, a);
		}
	}

	
}
